<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linguistic: Real Time TTS</title>
    <link rel="icon" href="{{ url_for('static', filename='favicon.png') }}" type="image/png">
    <style>
        /* General body styles */
        body {
            font-family: Arial, sans-serif;
            background: linear-gradient(45deg, #010758, #490d61);
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        /* Navigation bar styles */
        .navbar {
            width: 100%;
            font-size: larger;
            padding: 1rem 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 1000;
        }

        .navbar h1 {
            color: white;
            margin-left: 1rem;
        }

        .navbar a {
            color: white;
            text-decoration: none;
            padding: 0.5rem 1rem;
            margin: 0 0.5rem;
            border-radius: 4px;
            transition: background-color 0.3s;
        }

        .navbar a:hover {
            background-color: #555;
        }

        .navbar a:last-child {
            margin-right: 1rem;
        }

        .container {
            background-color: white;
            padding: 6rem 2rem;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            text-align: center;
            margin-top: 4rem;
        }

        h1 {
            color: #333;
        }

        form {
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        input[type="text"] {
            width: 100%;
            padding: 0.5rem;
            margin: 0.5rem 0;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 0.5rem 1rem;
            margin-top: 1rem;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        input[type="submit"]:hover {
            background-color: #45a049;
        }

        #audio-player {
            margin-top: 1rem;
        }

        select {
            width: 100%;
            padding: 0.5rem;
            margin: 0.5rem 0;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
    </style>
</head>

<body>
    <!-- Navigation Bar -->
    <div class="navbar">
        <div>
            <link rel="stylesheet"
                href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
            <a href="/" class="button"><i class="fa fa-home"></i></a>
            <a href="/tts" id="index-button">Voice Cloning</a>
        </div>
    </div>

    <div class="container">
        <h1>Real Time Text To Speech</h1>
        <form id="gtts-form" action="/gtts" method="post">
            <input type="text" id="text" name="text" placeholder="Enter text here" required>
            <select id="voice-select" name="voice">
                <!-- Options will be populated dynamically using JavaScript -->
            </select>
            <input type="submit" value="Convert to Speech">
        </form>
        <audio id="audio-player" controls style="display: none;">
            <source id="audio-source" src="" type="audio/mpeg">
            Your browser does not support the audio element.
        </audio>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            fetch('/gtts/voices')
                .then(response => response.json())
                .then(data => {
                    const voiceSelect = document.getElementById('voice-select');
                    data.voices.forEach(voice => {
                        const option = document.createElement('option');
                        option.value = voice.id;
                        option.textContent = voice.name;
                        voiceSelect.appendChild(option);
                    });
                })
                .catch(error => {
                    console.error('Error fetching voices:', error);
                });

            document.getElementById('gtts-form').onsubmit = function (event) {
                event.preventDefault();
                var formData = new FormData(event.target);

                fetch('/gtts', {
                    method: 'POST',
                    body: formData
                })
                    .then(response => response.json())
                    .then(data => {
                        if (data.audio_url) {
                            var audioPlayer = document.getElementById('audio-player');
                            var audioSource = document.getElementById('audio-source');
                            audioSource.src = data.audio_url + '?t=' + new Date().getTime();
                            audioPlayer.load();
                            audioPlayer.style.display = 'block';
                            audioPlayer.play();
                        } else if (data.error) {
                            alert(data.error);
                        }
                    })
                    .catch(error => {
                        console.error('Error:', error);
                    });
            };
        });
    </script>
</body>

</html>